package com.ymatou.iapi.prod.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prod.parameter.GetProductListByProductIdsBean;
import com.ymatou.iapi.prod.service.GetProductListByProductIdsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerAppProdService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetProductListByProductIds {
	
	private static GetProductListByProductIdsBean getProductListByProductIdsBean;
	private static GetProductListByProductIdsCall getProductListByProductIdsCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品列表");
	}

	@Before
	public void caseUp() throws Exception {
		getProductListByProductIdsBean = new GetProductListByProductIdsBean();
		getProductListByProductIdsCall = new GetProductListByProductIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductListByProductIdsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_001() {
		 Logger.start(true,"商品列表接口数据与mongodb验证");
		 try {
			 String productId_1 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 String productId_2 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 Thread.sleep(5000);
			 
			 ArrayList<String> list = new ArrayList<String>();
			 list.add(productId_1);
			 list.add(productId_2);
			 
			 //list.add("d8705d46-9a39-4ea4-8110-dcd544dbf817");
			 //list.add("f5f12def-e2cd-4d34-ada8-6d0ab7cb611c");
			 
			 getProductListByProductIdsBean.setProductIds(list);
			 getProductListByProductIdsBean.setUserId(4085);
			 
			 getProductListByProductIdsCall.setData(getProductListByProductIdsBean.toString());
			 getProductListByProductIdsCall.callService();

			 Logger.comment("getProductListByProductIdsCall send:" + getProductListByProductIdsCall.getEntityString());
			 Logger.comment("getProductListByProductIdsCall response:" + getProductListByProductIdsCall.getReturnData());
			
			 this.verifyNormalData(list, getProductListByProductIdsCall);
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_002() {
		 Logger.start(true,"验证规格的可享受的vip价格是否与mongodb一致");
		 try {
			 String productId_1 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 String productId_2 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 BuyerAppProdService prodService = new BuyerAppProdService();
			 
			 Thread.sleep(5000);
			 
			 ArrayList<String> list = new ArrayList<String>();
			 list.add(productId_1);
			 list.add(productId_2);
				 
			 getProductListByProductIdsBean.setProductIds(list);
			 getProductListByProductIdsBean.setUserId(20226821);
			 
			 getProductListByProductIdsCall.setData(getProductListByProductIdsBean.toString());
			 getProductListByProductIdsCall.callService();
			 
			 Logger.comment("getProductListByProductIdsCall send:" + getProductListByProductIdsCall.getEntityString());
			 Logger.comment("getProductListByProductIdsCall response:" + getProductListByProductIdsCall.getReturnData());
			 
			 JSONArray skus1 = new JSONArray(getProductListByProductIdsCall.getProduct(0).get("skus").toString());
			 String catalogId1 = new JSONObject(skus1.get(0).toString()).getString("id");
			 JSONArray skus2 = new JSONArray(getProductListByProductIdsCall.getProduct(1).get("skus").toString());
			 String catalogId2 = new JSONObject(skus2.get(0).toString()).getString("id");
			 
			 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(2, productId_1, catalogId1).getInt("Price"), 
					 new JSONObject(skus1.get(0).toString()).getInt("price"), "验证规格的可享受的vip价格是否与mongodb一致");
			 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(2, productId_2, catalogId2).getInt("Price"), 
					 new JSONObject(skus2.get(0).toString()).getInt("price"), "验证规格的可享受的vip价格是否与mongodb一致");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@SuppressWarnings("rawtypes")
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_003() {
		 Logger.start(true,"验证规格的可享受的新人价格是否与mongodb一致");
		 try {
			 Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			 
			 int userId = (Integer)user.get("userId");
			 String productId_1 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 String productId_2 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 BuyerAppProdService prodService = new BuyerAppProdService();
			 
			 Thread.sleep(5000);
			 
			 ArrayList<String> list = new ArrayList<String>();
			 list.add(productId_1);
			 list.add(productId_2);
				 
			 getProductListByProductIdsBean.setProductIds(list);
			 getProductListByProductIdsBean.setUserId(userId);
			 
			 getProductListByProductIdsCall.setData(getProductListByProductIdsBean.toString());
			 getProductListByProductIdsCall.callService();
			 
			 Logger.comment("getProductListByProductIdsCall send:" + getProductListByProductIdsCall.getEntityString());
			 Logger.comment("getProductListByProductIdsCall response:" + getProductListByProductIdsCall.getReturnData());
			 
			 this.verifyNormalData(list, getProductListByProductIdsCall);
			 
			 JSONArray skus1 = new JSONArray(getProductListByProductIdsCall.getProduct(0).get("skus").toString());
			 String catalogId1 = new JSONObject(skus1.get(0).toString()).getString("id");
			 JSONArray skus2 = new JSONArray(getProductListByProductIdsCall.getProduct(1).get("skus").toString());
			 String catalogId2 = new JSONObject(skus2.get(0).toString()).getString("id");
			 
			 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(1, productId_1, catalogId1).getInt("Price"), 
					 new JSONObject(skus1.get(0).toString()).getInt("price"), "验证规格的可享受的新人价格是否与mongodb一致");
			 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(1, productId_2, catalogId2).getInt("Price"), 
					 new JSONObject(skus2.get(0).toString()).getInt("price"), "验证规格的可享受的新人价格是否与mongodb一致");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	public void verifyNormalData(ArrayList<String> productIdList, 
			GetProductListByProductIdsCall getProductListByProductIdsCall) throws Exception {
		 int status = getProductListByProductIdsCall.getStatus();
		 int tariffType1 = 0;
		 int tariffType2 = 0; 
		 int length1 = 10086;
		 int length2 = 10086;
		 
		 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
		 BuyerAppProdService prodService = new BuyerAppProdService();
		 Map<String,Object> productDetailModelMap1 = productDetailModelWapper.getProductId(productIdList.get(0));
		 Map<String,Object> productDetailModelMap2 = productDetailModelWapper.getProductId(productIdList.get(1));
		 
		 JSONArray skus1 = new JSONArray(getProductListByProductIdsCall.getProduct(0).get("skus").toString());
		 String catalogId1 = new JSONObject(skus1.get(0).toString()).getString("id");
		 JSONArray catalogList1 = new JSONArray(productDetailModelMap1.get("CatalogList").toString());
		 JSONArray skus2 = new JSONArray(getProductListByProductIdsCall.getProduct(1).get("skus").toString());
		 String catalogId2 = new JSONObject(skus2.get(0).toString()).getString("id");
		 JSONArray catalogList2 = new JSONArray(productDetailModelMap2.get("CatalogList").toString());
		 
		 JSONArray propertyList1 = new JSONObject(catalogList1.get(0).toString()).getJSONArray("PropertyList");
		 JSONObject property1 = new JSONObject(propertyList1.getString(0));
		 JSONArray propertyList2 = new JSONObject(catalogList2.get(0).toString()).getJSONArray("PropertyList");
		 JSONObject property2 = new JSONObject(propertyList2.getString(0));
		 
		 Logger.comment("getProductInfoCall send:" + getProductListByProductIdsCall.getEntityString());
		 Logger.comment("getProductInfoCall response:" + getProductListByProductIdsCall.getReturnData());
		 
		 Logger.verifyEquals(200, status, "判断接口status是否为200");
		 Logger.verifyNotNull(getProductListByProductIdsCall.getResult(), "验证result是否为空");
		 Logger.verifyEquals(productIdList.size(), getProductListByProductIdsCall.getList().length(), "验证商品列表的大小");
		 Logger.verifyEquals(productDetailModelMap1.get("ProductId"), 
				 getProductListByProductIdsCall.getProduct(0).get("id"), "验证列表商品id是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap2.get("ProductId"), 
				 getProductListByProductIdsCall.getProduct(1).get("id"), "验证列表商品id是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap1.get("Title"), 
				 getProductListByProductIdsCall.getProduct(0).get("title"), "验证列表商品title是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap2.get("Title"), 
				 getProductListByProductIdsCall.getProduct(1).get("title"), "验证列表商品title是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap1.get("Title"), 
				 getProductListByProductIdsCall.getProduct(0).get("description"), "验证列表商品description是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap2.get("Title"), 
				 getProductListByProductIdsCall.getProduct(1).get("description"), "验证列表商品description是否与mongodb一致");
		 Logger.verifyEquals(this.getCST2TimeStamp(((Date) productDetailModelMap1.get("ValidStart")).toString()), 
				 this.changeUNIXTime(getProductListByProductIdsCall.getProduct(0).getLong("putawayTime")), "验证列表商品putawayTime是否与mongodb一致");
		 Logger.verifyEquals(this.getCST2TimeStamp(((Date) productDetailModelMap2.get("ValidStart")).toString()), 
				 this.changeUNIXTime(getProductListByProductIdsCall.getProduct(1).getLong("putawayTime")), "验证列表商品putawayTime是否与mongodb一致");
		 Logger.verifyEquals(this.getCST2TimeStamp(((Date) productDetailModelMap1.get("ValidEnd")).toString()), 
				 this.changeUNIXTime(getProductListByProductIdsCall.getProduct(0).getLong("expireTime")), "验证列表商品expireTime是否与mongodb一致");
		 Logger.verifyEquals(this.getCST2TimeStamp(((Date) productDetailModelMap2.get("ValidEnd")).toString()), 
				 this.changeUNIXTime(getProductListByProductIdsCall.getProduct(1).getLong("expireTime")), "验证列表商品expireTime是否与mongodb一致");
		 
		 if (productDetailModelMap1.get("TariffType").toString().equalsIgnoreCase("0") 
				 && productDetailModelMap1.get("Freight").toString().equalsIgnoreCase("0.00"))
			 tariffType1 = 3;
		 
		 if (productDetailModelMap2.get("TariffType").toString().equalsIgnoreCase("0") 
				 && productDetailModelMap2.get("Freight").toString().equalsIgnoreCase("0.00"))
			 tariffType2 = 3;
		 
		 Logger.verifyEquals(tariffType1, getProductListByProductIdsCall.getProduct(0).get("tariffType"), "验证列表商品tariffType是否与mongodb一致");
		 Logger.verifyEquals(tariffType2, getProductListByProductIdsCall.getProduct(1).get("tariffType"), "验证列表商品tariffType是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap1.get("DeliveryMethod"), 
				 getProductListByProductIdsCall.getProduct(0).get("deliveryType"), "验证列表商品deliveryType是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap2.get("DeliveryMethod"), 
				 getProductListByProductIdsCall.getProduct(1).get("deliveryType"), "验证列表商品deliveryType是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap1.get("SellerId"), 
				 getProductListByProductIdsCall.getProduct(0).get("sellerId"), "验证列表商品sellerId是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap2.get("SellerId"), 
				 getProductListByProductIdsCall.getProduct(1).get("sellerId"), "验证sellerId是否与mongodb一致");
		 Logger.verifyEquals(new JSONObject(catalogList1.get(0).toString()).getString("CatalogId"), 
				 new JSONObject(skus1.get(0).toString()).getString("id"), "验证列表商品CatalogId是否与mongodb一致");
		 Logger.verifyEquals(new JSONObject(catalogList2.get(0).toString()).getString("CatalogId"), 
				 new JSONObject(skus2.get(0).toString()).getString("id"), "验证列表商品CatalogId是否与mongodb一致");
		 Logger.verifyEquals(catalogList1.length(), skus1.length(), "验证列表商品Catalog size是否与mongodb一致");
		 Logger.verifyEquals(catalogList2.length(), skus2.length(), "验证列表商品Catalog size是否与mongodb一致");
		 
		 if (property1.get("Name").toString().equalsIgnoreCase("null") && property1.get("Value").toString().equalsIgnoreCase("null")
				 && property1.get("PicUrl").toString().equalsIgnoreCase("null"))
			 length1 = 0;
		 if (property2.get("Name").toString().equalsIgnoreCase("null") && property2.get("Value").toString().equalsIgnoreCase("null")
				 && property2.get("PicUrl").toString().equalsIgnoreCase("null"))
			 length2 = 0;
		 
		 Logger.verifyEquals(length1, 
				 new JSONObject(skus1.get(0).toString()).getJSONArray("properties").length(), "验证列表商品Catalog size是否与mongodb一致");
		 Logger.verifyEquals(length2, 
				 new JSONObject(skus2.get(0).toString()).getJSONArray("properties").length(), "验证列表商品Catalog size是否与mongodb一致");
		 Logger.verifyEquals("库存充足", 
				 new JSONObject(skus1.get(0).toString()).getString("stockDesc"), "验证商品列表库存大于5时的库存描述");
		 Logger.verifyEquals("库存充足", 
				 new JSONObject(skus2.get(0).toString()).getString("stockDesc"), "验证商品列表库存大于5时的库存描述");
		 Logger.verifyEquals(prodService.getCatalogStockFromMongo(0, productIdList.get(0), catalogId1).getInt("Stock"), 
				 new JSONObject(skus1.get(0).toString()).getInt("stock"), "验证商品列表规格的库存数量是否已mongodb一致");
		 Logger.verifyEquals(prodService.getCatalogStockFromMongo(0, productIdList.get(1), catalogId2).getInt("Stock"), 
				 new JSONObject(skus2.get(0).toString()).getInt("stock"), "验证商品列表规格的库存数量是否已mongodb一致");
		 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(0, productIdList.get(0), catalogId1).getInt("Price"), 
				 new JSONObject(skus1.get(0).toString()).getInt("original"), "验证商品列表规格的原价是否与mongodb一致");
		 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(0, productIdList.get(1), catalogId2).getInt("Price"), 
				 new JSONObject(skus2.get(0).toString()).getInt("original"), "验证商品列表规格的原价是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap1.get("NoReasonReturn"), 
				 getProductListByProductIdsCall.getProduct(0).getBoolean("isNoReasonReturn"), "验证isNoReasonReturn是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap2.get("NoReasonReturn"), 
				 getProductListByProductIdsCall.getProduct(1).getBoolean("isNoReasonReturn"), "验证isNoReasonReturn是否与mongodb一致");
	}
	
	//将unix时间戳毫秒数置为000
	public long changeUNIXTime(long timeStamp) {
		return Long.parseLong(String.valueOf(timeStamp).substring(0, 10).concat("000"));
	}
	
	public long getCST2TimeStamp(String dateStr) {
		return YMTDateUtil.CST2TimeStamp(dateStr);
	}
}
